package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.umakr.ax.common.mapper.UserScoreMapper;
import com.umakr.ax.common.model.UserScore;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.core.properties.PropertiesModel;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import com.umakr.ax.utils.WebUtil;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 积分 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class UserScoreService extends BaseService<UserScoreMapper, UserScore>{

    @Autowired
    PropertiesModel propertiesModel;
    @Autowired
    Environment env;
    private static final String RS_LOGO = "rsLogo";
    private static final String SCORE_TYPE = "scoreType";
    private static final String SCORE_TYPE_NAME = "scoreTypeName";
    private static final String SCORE_TIME = "scoreTime";

    ThreadPoolExecutor cachedThreadPool = new ThreadPoolExecutor(20,500,1, TimeUnit.MINUTES,new LinkedBlockingDeque<Runnable>());

    public Object listRsScoreByUserId(Long userId ){
        String aliUrl = env.getProperty("oss.img.path");

        Page<Map<String,Object>> page = baseMapper.listRsScoreByUserId(userId);

        Integer totalScore = 0;

        for(Map<String,Object> map :page.getRecords()){
            if(map.get(RS_LOGO) == null) {
                map.put(RS_LOGO,"");
            }else {
                map.put(RS_LOGO, FormatPhotoUrlUtil.formatString(aliUrl, map.get(RS_LOGO)));
            }
            totalScore += Integer.parseInt(map.get("score").toString());
        }

        WebUtil.addParamToPage(page,"totalScore",totalScore);

        return page;
    }

    public Object listScoreByRsId(Long rsId ){

        Page<Map<String,Object>> page = baseMapper.listScoreByRsId(rsId);

        for(Map<String,Object> map :page.getRecords()){
            if(map.get(SCORE_TYPE).equals(1)){
                map.put(SCORE_TYPE_NAME,"互动");
            }else if(map.get(SCORE_TYPE).equals(0)){
                map.put(SCORE_TYPE_NAME,"活动");
            }

            if(map.get(SCORE_TIME) == null) {
                map.put(SCORE_TIME,"");
            }
        }

        return page;
    }

    public Object listScoreByRsIdAndUserId(Long rsId ,Long userId){

        Page<Map<String,Object>> page = baseMapper.listScoreByRsIdAndUserId(rsId,userId);

        for(Map<String,Object> map :page.getRecords()){
            if(map.get(SCORE_TYPE).equals(1)){
                map.put(SCORE_TYPE_NAME,"互动");
            }else if(map.get(SCORE_TYPE).equals(0)){
                map.put(SCORE_TYPE_NAME,"活动");
            } else {
                map.put(SCORE_TYPE_NAME,"系统奖励");
            }

            if(map.get(SCORE_TIME) == null) {
                map.put(SCORE_TIME,"");
            }
        }

        return page;
    }

    /**
     * 自动累计加分
     * @param userId
     * @param rsId
     * @param scoreType
     */
    public Object autoAddUpScore(Long userId,Long rsId,Integer scoreType){
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                Date date = new Date();
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

                Integer count = baseMapper.countByUserIdAndRsIdAndDate(userId,sf.format(date));

                if(count < propertiesModel.getMaxScoreTimes()){
                    List<Integer> scoreTypeList = propertiesModel.getScoreType();
                    Integer score = scoreTypeList.get(scoreType);
                    UserScore us = new UserScore();
                    us.setRsId(rsId);
                    us.setUserId(userId);
                    us.setScore(score);
                    us.setScoreType(scoreType);
                    us.setSocreTime(date);
                    update(us);

                }

            }
        });
        return resultMap(1,"");
    }

}
